package com.tbit.main.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.tbit.main.constant.RedisConstant;
import com.tbit.main.pojo.Result;
import com.tbit.main.service.RedisService;
import com.tbit.manager.api.UsageApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用量提醒
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2024-10-22
 */
@RestController
@RequestMapping("/usage")
@Slf4j
public class UsageController {

    @Autowired
    private UsageApi usageApi;
    @Resource
    private RedisService redisService;

    /**
     * 查询提醒信息
     * @param brandId 品牌id
     * @return Result
     */
    @GetMapping("/getMessage")
    public Result<Map<String, Object>> getMessage(Integer brandId, Integer userId){
        if(redisService.exists(RedisConstant.USAGE_KEY + brandId + "_" + userId)){
            return Result.success();
        }
        return Result.success(usageApi.getMessage(brandId));
    }


    /**
     * 设置三日内不提醒
     * @param brandId   品牌id
     * @return  Object
     */
    @GetMapping("/donTRemindMeToday")
    public Result<Object> donTRemindMeToday(Integer brandId, Integer userId){
        try {
            DateTime date = DateUtil.date();
            DateTime threeDate = DateUtil.beginOfDay(DateUtil.offsetDay(date, 3));
            double mul = NumberUtil.mul(threeDate.getTime(), date.getTime());
            redisService.add(RedisConstant.USAGE_KEY + brandId + "_" + userId, "1", (int)mul);
            return Result.success();
        }catch (Exception e){
            log.error("设置失败", e);
        }
        return Result.error();
    }
}
